home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’89 / PopUpCtl / TestControl.c < prev    next >
C/C++ Source or Header  |  1988-07-20  |  3KB  |  151 lines

  1. #include        <types.h>
  2. #include        <quickdraw.h>
  3. #include        <windows.h>
  4. #include        <controls.h>
  5. #include        <menus.h>
  6. #include        <dialogs.h>
  7. #include        <resources.h>
  8. #include        <OSevents.h>
  9. #include        <script.h>
  10. #include        <fonts.h>
  11. #include        <Events.h>
  12. #include        <textedit.h>
  13. #include        <OSUtils.h>
  14. #include        <Traps.h>
  15.  
  16. #define            NIL            ((void *) 0l)
  17.  
  18. pascal void Debugger() extern 0xa9ff;
  19.  
  20. void        DoDialog();
  21. Boolean        DialogExists();
  22. void        PositionDialog();
  23. void        SetItemValue();
  24. int            GetItemValue();
  25. void        HiliteItem();
  26. Handle        GetItemHandle();
  27.  
  28. main()
  29. {
  30.     FlushEvents (everyEvent - diskMask, 0 );
  31.     InitGraf (&qd.thePort);
  32.     InitFonts ();
  33.     InitWindows ();
  34.     InitMenus ();
  35.     TEInit ();
  36.     InitDialogs (nil);        /* no restart proc */
  37.     InitCursor ();
  38.  
  39.     DoDialog();
  40. }
  41.  
  42. void DoDialog()
  43. {
  44.     short        id;
  45.     DialogPtr    dlog;
  46.     
  47.     if(!DialogExists(256))
  48.     {
  49.         SysBeep(3);
  50.         return;
  51.     }
  52.     
  53.     dlog = GetNewDialog(256, NULL, (WindowPtr) -1);
  54.     
  55.     PositionDialog(dlog);
  56.     
  57.     SetItemValue(dlog, 2, 1);
  58.     
  59.     ShowWindow(dlog);
  60.     
  61.     do
  62.     {
  63.         ModalDialog(NULL, &id);
  64.         if(id == 2)
  65.         {
  66.             SetItemValue(dlog, 2, !GetItemValue(dlog, 2));
  67.             HiliteItem(dlog, 3, GetItemValue(dlog, 2) ? 0 : 255);
  68.         }
  69.     } while(id > ok);
  70.     
  71.     DisposDialog(dlog);
  72. }
  73.  
  74. Handle    GetItemHandle(dlog, item, type, rect)
  75.     DialogPtr    dlog;
  76.     int            item;
  77.     short        *type;
  78.     Rect        *rect;
  79. {
  80.     Rect    r;
  81.     short    t;
  82.     Handle    hand;
  83.  
  84.     if(!rect)
  85.         rect = &r;
  86.     if(!type)
  87.         type = &t;
  88.     GetDItem(dlog, item, type, &hand, rect);
  89.     
  90.     return hand;
  91. }
  92.  
  93. GetItemValue(dlog, item)
  94.     DialogPtr    dlog;
  95.     int            item;
  96. {
  97.     return GetCtlValue(GetItemHandle(dlog, item, NIL, NIL));
  98. }
  99.  
  100. void SetItemValue(dlog, item, value)
  101.     DialogPtr    dlog;
  102.     int            item;
  103. {
  104.     SetCtlValue(GetItemHandle(dlog, item, NIL, NIL), value);
  105. }
  106.  
  107. void HiliteItem(dlog, item, value)
  108.     DialogPtr    dlog;
  109.     int            item;
  110.     int            value;
  111. {
  112.     Rect        rect;
  113.     short        type;
  114.     Handle        hand;
  115.     
  116.     hand = GetItemHandle(dlog, item, &type, &rect);
  117.  
  118.     type &= ~itemDisable;
  119.     if((type & ~3) == ctrlItem)
  120.         HiliteControl(hand, value);
  121. }
  122.  
  123. void PositionDialog(dptr)
  124. DialogPtr        dptr;
  125. {
  126.     GrafPtr                wPort;
  127.     int                    hPos, vPos;
  128.     Rect                wRect, dRect;
  129.     
  130.     GetWMgrPort(&wPort);
  131.     wRect = wPort->portRect;
  132.     wRect.top += GetMBarHeight();
  133.     dRect = dptr->portRect;
  134.     hPos = (wRect.left + wRect.right + dRect.left - dRect.right) / 2;
  135.     if(hPos < (wRect.left + 3))
  136.         hPos = wRect.left + 3;
  137.     vPos = (2 * wRect.top + wRect.bottom + dRect.top - dRect.bottom) / 3;
  138.     if(vPos < (wRect.top + 3))
  139.         vPos = wRect.top + 3;
  140.     MoveWindow(dptr, hPos, vPos, false);
  141. }
  142.  
  143. Boolean DialogExists(id)
  144.     short    id;
  145. {
  146.     DialogTHndl    hand;
  147.     if(!(hand = (DialogTHndl) GetResource('DLOG', id)))
  148.         return 0 != 0;
  149.     return GetResource('DITL', (*hand)->itemsID) != 0;
  150. }
  151.